﻿#include "WRWorkDomainsGuiView.h"

#include <QGridLayout>

static const QString g_openString = QStringLiteral("打开");
static const QString g_closeString = QStringLiteral("关闭");

//初始化界面部件。
WRWorkDomainsGuiView::WRWorkDomainsGuiView(const WRObjRef<WRWorkDaemon>& daemon, QWidget* parent)
    : QSplitter(parent)
{
    //标题文本
    m_titleLabel = new QLabel();
    m_titleLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    //状态标签
    m_statusLabel = new QLabel();
    m_statusLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    //切换按钮
    m_switchButton = new QPushButton(g_closeString);
    QObject::connect(m_switchButton, SIGNAL(clicked(bool)), this, SLOT(onButtonClicked()));
    //文本显示框
    m_descriptionBox = new QTextEdit();
    m_descriptionBox->setReadOnly(true);
    //表格视图
    m_tableView = new QTableView();
    m_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

    QGridLayout* gridLayout = new QGridLayout();
    gridLayout->addWidget(m_titleLabel, 0, 0, 1, 2, Qt::AlignLeft);
    gridLayout->addWidget(m_statusLabel, 1, 0, 1, 1, Qt::AlignLeft);
    gridLayout->addWidget(m_switchButton, 1, 1, 1, 1, Qt::AlignRight);
    gridLayout->addWidget(m_descriptionBox, 2, 0, 1, 2);
    gridLayout->setRowStretch(0, 0);
    gridLayout->setRowMinimumHeight(0, 24);
    gridLayout->setRowStretch(1, 0);
    gridLayout->setRowMinimumHeight(1, 24);
    gridLayout->setRowStretch(2, 1);
    gridLayout->setSpacing(0);
    gridLayout->setContentsMargins(0, 0, 0, 0);

    QWidget* widget = new QWidget();
    widget->setLayout(gridLayout);

    addWidget(m_tableView);
    addWidget(widget);

    m_listModel = new WRWorkDomainListModel(daemon);
    m_tableView->setModel(m_listModel);
    m_tableView->clearSelection();
    m_tableView->selectRow(0);
    m_current = 0;
    updateCurrent();
    QObject::connect(
                m_listModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
                this, SLOT(onDataChanged(QModelIndex,QModelIndex)));
    QObject::connect(
                m_tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
                this, SLOT(onCurrentRowChanged(QModelIndex,QModelIndex)));
}
//释放当前对象所占用的相关资源。
WRWorkDomainsGuiView::~WRWorkDomainsGuiView(void)
{
    m_listModel->inactivate();
    m_listModel->deleteLater();
}

//打开界面显示。
void WRWorkDomainsGuiView::open(void)
{
    m_listModel->activate();
}
//关闭界面显示。
void WRWorkDomainsGuiView::close(void)
{
    m_listModel->inactivate();
}

//更新当前选中的信息。
void WRWorkDomainsGuiView::updateCurrent(void)
{
    WRWorkDomainListEntry* item = m_listModel->index(m_current);
    m_titleLabel->setText(item->title());
    m_descriptionBox->setText(item->description());
    WRWorkDomain::Status status = item->status();
    m_statusLabel->setText(WRText::toString(status));
    if (status == WRWorkDomain::Closed)
    {
        m_switchButton->setText(g_openString);
    }
    else
    {
        m_switchButton->setText(g_closeString);
    }
}

//当按钮点击后调用此方法。
void WRWorkDomainsGuiView::onButtonClicked(void)
{
    WRWorkDomainListEntry* item = m_listModel->index(m_current);
    if (item->status() == WRWorkDomain::Closed)
    {
        item->start();
    }
    else
    {
        item->close();
    }
}
//当数据改变后调用此方法。
void WRWorkDomainsGuiView::onDataChanged(const QModelIndex &topLeft, const QModelIndex &)
{
    if (topLeft.row() == m_current)
    {
        updateCurrent();
    }
}
//当选择行改变后调用此方法。
void WRWorkDomainsGuiView::onCurrentRowChanged(const QModelIndex &current, const QModelIndex &)
{
    m_current = current.row();
    updateCurrent();
}
